import { queryPage, queryInfoById, create, update, queryBuildPage, queryPersonPage, queryTaskRuleTargetPage, queryRuleTargetDetail, queryFireRoomPage } from './service';
import { queryDictList } from '@/services/dict';
import { routerRedux } from 'dva/router';
import { DICT } from './enum';

export default {
  namespace: 'unit_task_rules',

  state: {
    list: { data: [] },
    details: [],
    cond: {},
    dict: {}
  },

  effects: {
    *initCond(_, { put }) {
      yield put({ type: 'setCond', payload: {resType: '1'} });
      yield put({ type: 'queryPage' })
    },
    *updateCond({ payload }, { put }) {
      yield put({ type: 'setCond', payload });
      yield put({ type: 'queryPage' });
    },
    *queryPage({ payload = { pageNum: 1, pageSize: 10 } }, { call, put, select }) {
      const cond = yield select(state => state.unit_task_rules.cond);
      const params = { ...payload, ...cond };
      const { list = [], total } = yield call(queryPage, params);
      yield put({ type: 'queryDict', payload: {keyList: DICT} });
      yield put({ type: 'setPage', payload: { list, total } })
    },
    *queryDict({ payload }, { call, put }) {
      const dict = yield call(queryDictList, payload);
      yield put({ type: 'setDict', payload: dict })
    },
    *queryInfoById({ payload }, { call, put }) {
      const response = yield call(queryInfoById, payload);
      yield put({ type: 'setInfo', payload: response })
    },
    *create({ payload }, { call, put }) {
      yield call(create, payload);
      yield put(routerRedux.push('/data/unit_task_rules'));
    },
    *update({ payload }, { call, put }) {
      yield call(update, payload);
      yield put(routerRedux.push('/data/unit_task_rules'));
    },
    *updateBuildCond({ payload = { pageNum: 1, pageSize: 10 } }, { put }) {
      const { pageSize, pageNum, ...cond } = payload;
      yield put({ type: 'setBuildCond', payload: cond });
      yield put({ type: 'queryBuildPage', payload });
    },
    *queryBuildPage({ payload }, { call, put, select }) {
      const cond = yield select(state => state.unit_task_rules.buildCond);
      const params = { ...payload, ...cond };
      const { list = [], total } = yield call(queryBuildPage, params);
      yield put({ type: 'getBuildPage', payload: { list, total } })
    },
    *updateFireRoomCond({ payload = { pageNum: 1, pageSize: 10 } }, { put }) {
      const { pageSize, pageNum, ...cond } = payload;
      yield put({ type: 'setFireRoomCond', payload: cond });
      yield put({ type: 'queryFireRoomPage', payload });
    },
    *queryFireRoomPage({ payload }, { call, put, select }) {
      const cond = yield select(state => state.unit_task_rules.fireRoomCond);
      const params = { ...payload, ...cond };
      const { list = [], total } = yield call(queryFireRoomPage, params);
      yield put({ type: 'getFireRoomPage', payload: { list, total } })
    },
    *updatePersonCond({ payload = { pageNum: 1, pageSize: 10 } }, { put }) {
      const { pageSize, pageNum, ...cond } = payload;
      yield put({ type: 'setPersonCond', payload: cond });
      yield put({ type: 'queryPersonPage', payload });
    },
    *queryPersonPage({ payload }, { call, put, select }) {
      const cond = yield select(state => state.unit_task_rules.personCond);
      const params = { ...payload, ...cond };
      const { list = [], total } = yield call(queryPersonPage, params);
      yield put({ type: 'getPersonPage', payload: { list, total } })
    },
    *updateTaskRuleTargetCond({ payload = { pageNum: 1, pageSize: 10 } }, { put }) {
      const { pageSize, pageNum, ...cond } = payload;
      yield put({ type: 'setTaskRuleTargetCond', payload: cond });
      yield put({ type: 'queryTaskRuleTargetPage', payload });
    },
    *queryTaskRuleTargetPage({ payload }, { call, put, select }) {
      const cond = yield select(state => state.unit_task_rules.taskRuleTargetCond);
      const params = { ...payload, ...cond };
      const { list = [], total } = yield call(queryTaskRuleTargetPage, params);
      yield put({ type: 'getTaskRuleTargetPage', payload: { list, total } })
    },
    *updateBuildTargetCond({ payload = { pageNum: 1, pageSize: 10 } }, { put }) {
      const { pageSize, pageNum, ...cond } = payload;
      yield put({ type: 'setBuildTargetCond', payload: cond });
      yield put({ type: 'queryBuildTargetPage', payload });
    },
    *queryBuildTargetPage({ payload }, { call, put, select }) {
      const cond = yield select(state => state.unit_task_rules.buildTargetCond);
      const params = { ...payload, ...cond };
      const { list = [], total } = yield call(queryTaskRuleTargetPage, params);
      yield put({ type: 'getBuildTargetPage', payload: { list, total } })
    },
    *updateFireRoomTargetCond({ payload = { pageNum: 1, pageSize: 10 } }, { put }) {
      const { pageSize, pageNum, ...cond } = payload;
      yield put({ type: 'setFireRoomTargetCond', payload: cond });
      yield put({ type: 'queryFireRoomTargetPage', payload });
    },
    *queryFireRoomTargetPage({ payload }, { call, put, select }) {
      const cond = yield select(state => state.unit_task_rules.fireRoomTargetCond);
      const params = { ...payload, ...cond };
      const { list = [], total } = yield call(queryTaskRuleTargetPage, params);
      yield put({ type: 'getFireRoomTargetPage', payload: { list, total } })
    },
    *getRuleTargetDetail({ payload }, { call, put }) {
      const response = yield call(queryRuleTargetDetail, payload);
      yield put({ type: 'ruleDetails', payload: response })
    }
  },

  reducers: {
    setCond(state, action) {
      return {
        ...state,
        cond: action.payload
      }
    },
    setBuildCond(state, action) {
      return {
        ...state,
        buildCond: action.payload
      }
    },
    setFireRoomCond(state, action) {
      return {
        ...state,
        fireRoomCond: action.payload
      }
    },
    setPersonCond(state, action) {
      return {
        ...state,
        personCond: action.payload
      }
    },
    setTaskRuleTargetCond(state, action) {
      return {
        ...state,
        taskRuleTargetCond: action.payload
      }
    },
    setBuildTargetCond(state, action) {
      return {
        ...state,
        buildTargetCond: action.payload
      }
    },
    setFireRoomTargetCond(state, action) {
      return {
        ...state,
        fireRoomTargetCond: action.payload
      }
    },
    setPage(state, action) {
      return {
        ...state,
        list: {
          data: action.payload.list,
          dataCnt: action.payload.total
        }
      }
    },
    setDict(state, action) {
      return {
        ...state,
        dict: action.payload
      }
    },
    setInfo(state, action) {
      return {
        ...state,
        details: action.payload
      }
    },
    getBuildPage(state, action) {
      return {
        ...state,
        buildList: {
          data: action.payload.list,
          dataCnt: action.payload.total
        }
      }
    },
    getPersonPage(state, action) {
      return {
        ...state,
        personList: {
          data: action.payload.list,
          dataCnt: action.payload.total
        }
      }
    },
    getFireRoomPage(state, action) {
      return {
        ...state,
        fireRoomList: {
          data: action.payload.list,
          dataCnt: action.payload.total
        }
      }
    },
    getTaskRuleTargetPage(state, action) {
      return {
        ...state,
        taskRuleTargetList: {
          data: action.payload.list,
          dataCnt: action.payload.total
        }
      }
    },
    getBuildTargetPage(state, action) {
      return {
        ...state,
        buildTargetList: {
          data: action.payload.list,
          dataCnt: action.payload.total
        }
      }
    },
    getFireRoomTargetPage(state, action) {
      return {
        ...state,
        fireRoomTargetList: {
          data: action.payload.list,
          dataCnt: action.payload.total
        }
      }
    },
    ruleDetails(state, action) {
      return {
        ...state,
        ruleDetails: action.payload
      }
    }
  }
}
